﻿// Decompiled with JetBrains decompiler
// Type: StoryMode.StoryModePhases.SecondPhase
// Assembly: StoryMode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 11BAE65F-6C15-4628-A9C6-1B968588CDA1
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\StoryMode\bin\Win64_Shipping_Client\StoryMode.dll

using StoryMode.Quests.SecondPhase;
using StoryMode.Quests.SecondPhase.ConspiracyQuests;
using StoryMode.StoryModeObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.SceneInformationPopupTypes;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.SaveSystem;

#nullable disable
namespace StoryMode.StoryModePhases
{
  public class SecondPhase
  {
    public const int MaxConspiracyStrength = 2000;
    public const float DailyConspiracyChange = 2.777777f;
    public const int ConspiracyQuestDurationAsWeeks = 3;
    [SaveableField(3)]
    private int _stopConspiracyAttempts;
    [SaveableField(4)]
    private ConspiracyQuestBase _lastConspiracyQuest;
    private MBList<Type> _conspiracyQuestTypes;

    internal static void AutoGeneratedStaticCollectObjectsSecondPhase(
      object o,
      List<object> collectedObjects)
    {
      ((StoryMode.StoryModePhases.SecondPhase) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected virtual void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      collectedObjects.Add((object) this._lastConspiracyQuest);
      CampaignTime.AutoGeneratedStaticCollectObjectsCampaignTime((object) this.LastConspiracyQuestCreationTime, collectedObjects);
    }

    internal static object AutoGeneratedGetMemberValueLastConspiracyQuestCreationTime(object o)
    {
      return (object) ((StoryMode.StoryModePhases.SecondPhase) o).LastConspiracyQuestCreationTime;
    }

    internal static object AutoGeneratedGetMemberValueConspiracyStrength(object o)
    {
      return (object) ((StoryMode.StoryModePhases.SecondPhase) o).ConspiracyStrength;
    }

    internal static object AutoGeneratedGetMemberValue_stopConspiracyAttempts(object o)
    {
      return (object) ((StoryMode.StoryModePhases.SecondPhase) o)._stopConspiracyAttempts;
    }

    internal static object AutoGeneratedGetMemberValue_lastConspiracyQuest(object o)
    {
      return (object) ((StoryMode.StoryModePhases.SecondPhase) o)._lastConspiracyQuest;
    }

    public static StoryMode.StoryModePhases.SecondPhase Instance
    {
      get => StoryModeManager.Current.MainStoryLine.SecondPhase;
    }

    [SaveableProperty(1)]
    public CampaignTime LastConspiracyQuestCreationTime { get; private set; }

    [SaveableProperty(5)]
    public float ConspiracyStrength { get; private set; }

    public SecondPhase()
    {
      this.LastConspiracyQuestCreationTime = CampaignTime.Never;
      this.ConspiracyStrength = 1000f;
      this._stopConspiracyAttempts = 0;
      this._lastConspiracyQuest = (ConspiracyQuestBase) null;
      this.InitializeConspiracyQuestTypes();
      this.SetTransferableOfConspiracyTroops();
    }

    public void OnSessionLaunched()
    {
      this.InitializeConspiracyQuestTypes();
      this.SetTransferableOfConspiracyTroops();
    }

    private void InitializeConspiracyQuestTypes()
    {
      MBList<Type> mbList = new MBList<Type>();
      mbList.Add(typeof (DestroyRaidersConspiracyQuest));
      mbList.Add(typeof (ConspiracyBaseOfOperationsDiscoveredConspiracyQuest));
      mbList.Add(typeof (DisruptSupplyLinesConspiracyQuest));
      this._conspiracyQuestTypes = mbList;
    }

    private void SetTransferableOfConspiracyTroops()
    {
      foreach (PartyTemplateStack partyTemplateStack in Campaign.Current.ObjectManager.GetObject<PartyTemplateObject>("conspiracy_anti_imperial_special_raider_party_template").Stacks.Concat<PartyTemplateStack>((IEnumerable<PartyTemplateStack>) Campaign.Current.ObjectManager.GetObject<PartyTemplateObject>("conspiracy_imperial_special_raider_party_template").Stacks))
        partyTemplateStack.Character.SetTransferableInPartyScreen(false);
      Game.Current.ObjectManager.GetObject<CharacterObject>("conspiracy_commander_antiempire").SetTransferableInPartyScreen(false);
      Game.Current.ObjectManager.GetObject<CharacterObject>("conspiracy_commander_empire").SetTransferableInPartyScreen(false);
    }

    public void TriggerConspiracy()
    {
      this.LastConspiracyQuestCreationTime = CampaignTime.Now;
      if (this._lastConspiracyQuest != null)
        return;
      MBInformationManager.ShowSceneNotification((SceneNotificationData) new EmpireConspiracyBeginsSceneNotificationItem(Hero.MainHero, StoryModeManager.Current.MainStoryLine.PlayerSupportedKingdom, StoryModeManager.Current.MainStoryLine.IsOnImperialQuestLine));
    }

    public void IncreaseConspiracyStrength()
    {
      this.ConspiracyStrength += 2.777777f;
      if ((double) this.ConspiracyStrength > 2000.0)
        this.ConspiracyStrength = 2000f;
      if ((double) this.ConspiracyStrength < 2000.0)
        return;
      this.ActivateConspiracy();
    }

    public void DecreaseConspiracyStrength(float amount) => this.ConspiracyStrength -= amount;

    public void ActivateConspiracy()
    {
      StoryModeManager.Current.MainStoryLine.CompleteSecondPhase();
    }

    public void CreateNextConspiracyQuest()
    {
      Type type = this._lastConspiracyQuest == null ? this._conspiracyQuestTypes.GetRandomElement<Type>() : this._conspiracyQuestTypes.GetRandomElementWithPredicate<Type>((Func<Type, bool>) (t => t != this._lastConspiracyQuest.GetType()));
      ++this._stopConspiracyAttempts;
      object[] objArray = new object[2]
      {
        (object) ("conspiracy_quest_" + (object) this._stopConspiracyAttempts),
        StoryModeManager.Current.MainStoryLine.IsOnAntiImperialQuestLine ? (object) StoryModeHeroes.AntiImperialMentor : (object) StoryModeHeroes.ImperialMentor
      };
      this._lastConspiracyQuest = (ConspiracyQuestBase) Activator.CreateInstance(type, objArray);
      this._lastConspiracyQuest.StartQuest();
      this.TriggerConspiracy();
    }
  }
}
